package LW.cercle;

import java.io.IOException;

import javax.microedition.khronos.opengles.GL10;

import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.handler.physics.PhysicsHandler;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
import org.anddev.andengine.entity.Entity;
import org.anddev.andengine.entity.IEntity;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.scene.background.IBackground;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.extension.physics.box2d.PhysicsFactory;
import org.anddev.andengine.extension.physics.box2d.PhysicsWorld;
import org.anddev.andengine.extension.ui.livewallpaper.BaseLiveWallpaperService;
import org.anddev.andengine.opengl.texture.ITexture;
import org.anddev.andengine.opengl.texture.ITexture.ITextureStateListener;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.Texture.PixelFormat;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BuildableBitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import org.anddev.andengine.opengl.texture.source.ITextureAtlasSource;
import org.anddev.andengine.opengl.util.GLHelper;

import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.FixtureDef;

import android.content.SharedPreferences;
import android.graphics.Point;
import android.provider.MediaStore.Images;
import android.util.Log;


public class CercleLW  extends BaseLiveWallpaperService{
	
	
	private static final int LAYER_COUNT = 2;
	private static final int LAYER_BACKGROUND = 0;
	private static final int LAYER_BALL = 1;
	private static final int CAMERA_WIDTH = 480;
	private static final int CAMERA_HEIGHT = 800;
	private static final float RAYON = 283;
	private static final float INIT_X = CAMERA_WIDTH/2;
	private static final float INIT_Y = CAMERA_HEIGHT/2;
	private static final float VITESSE = (float) 0.1;
	private static long animationTime_ = 0;
	private static float elapsedTimeSum_ = 0;
	private float angle_ = 0;
	
	private static final FixtureDef FIXTURE_DEF = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f);
	//private TiledTextureRegion circleFaceTextureRegion_;
	private Scene scene_;
	private Body ballBody_;
	private AnimatedSprite ball_;
	private Sprite contour_;
	private PhysicsWorld physicsWorld_;
	private BitmapTextureAtlas bitmapTextureAtlas_;
	private FixtureDef wallFixtureDef_;
	
	private BitmapTextureAtlas backgroundTexture_;
	private TextureRegion backgroundTextureRegion_;
	
	private BitmapTextureAtlas contourTexture_;
	private TextureRegion contourTextureRegion_;
	private TiledTextureRegion circleFaceTextureRegion_;
	
	@Override
	public org.anddev.andengine.engine.Engine onLoadEngine() {
		Log.d("TAG_Labyrinthe", "onLoadEngine");
		Camera mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		return new org.anddev.andengine.engine.Engine(new EngineOptions(true, ScreenOrientation.PORTRAIT, new FillResolutionPolicy(), mCamera));
	}
	
	@Override
	public void onLoadResources() {
		bitmapTextureAtlas_ = new BitmapTextureAtlas(1024, 1024, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		circleFaceTextureRegion_ = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(bitmapTextureAtlas_, this, "Infinity_ball.png", 0, 0, 1, 1); // 64x32
		//circleFaceTextureRegion_ = BitmapTextureAtlasTextureRegionFactory.createFromAsset(bitmapTextureAtlas_, this, "Infinity_ball.png", 0, 0); // 64x32
		
		backgroundTexture_ = new BitmapTextureAtlas(1024, 1024, TextureOptions.DEFAULT);
		backgroundTextureRegion_ = BitmapTextureAtlasTextureRegionFactory.createFromAsset(backgroundTexture_, this, "Infinity.png", 0, 0);

		contourTexture_ = new BitmapTextureAtlas(1024, 1024, TextureOptions.DEFAULT);
		contourTextureRegion_ = BitmapTextureAtlasTextureRegionFactory.createFromAsset(contourTexture_, this, "masque.png", 0, 0);

		this.mEngine.getTextureManager().loadTexture(bitmapTextureAtlas_);
		this.mEngine.getTextureManager().loadTexture(backgroundTexture_);
		this.mEngine.getTextureManager().loadTexture(contourTexture_);
	}

	@Override
	public void onUnloadResources() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Scene onLoadScene() {
		// TODO Auto-generated method stub
		scene_ = new Scene();
		
		ball_ = new AnimatedSprite(INIT_X, INIT_Y, circleFaceTextureRegion_);
		contour_ = new Sprite(CAMERA_WIDTH/2 - 120, CAMERA_HEIGHT/2 - 120, contourTextureRegion_);
		
		PhysicsHandler p = new PhysicsHandler(ball_){
			@Override
			protected void onUpdate(float pSecondsElapsed, IEntity pEntity) {
				
				Point p = calculPoint(pSecondsElapsed);
				ball_.setPosition(p.x, p.y);
				/*if(angle_ > (0) && angle_ < (Math.PI)){
					if(scene_.getChild(2) != ball_){
						scene_.detachChild(ball_);
						scene_.attachChild(ball_);
					}
				}
				else{
					if(scene_.getChild(2) != contour_){
						scene_.detachChild(contour_);
						scene_.attachChild(contour_);
					}
				}*/
				elapsedTimeSum_ += pSecondsElapsed;
				//ATTENTION: si animationTime_ devient plus grand que pSecondsElapdsed il n'y a plus d'animation
				/*if(elapsedTimeSum_ > 0.1){
					animationTime_++;
					ball_.animate(animationTime_);
					elapsedTimeSum_ = 0;
				}*/
				//Log.d("TAG_Cercle", "x:" + p.x + " y:" + p.y);
				super.onUpdate(pSecondsElapsed, pEntity);
			}
		};
		
		scene_.registerUpdateHandler(p);
		scene_.attachChild(new Sprite(35, 17, backgroundTextureRegion_));
		scene_.attachChild(ball_);
		//scene_.attachChild(contour_);
		
		return scene_;
	}

	private Point calculPoint(float elapsedTime){
		Point p = new Point();
		angle_ += (elapsedTime)*2*Math.PI*VITESSE;
		
		if(angle_ > 2*Math.PI)
			angle_ -= 2*Math.PI;
		/*	EQUATION PARAMETRIC DU CERCLE
		p.x = (int) (INIT_X + RAYON*Math.cos(angle_));
		p.y = (int) (INIT_Y + RAYON*Math.sin(angle_));
		*/
		
		//EQUATION PARAMETRIC DU SYMBOLE INFINI
		p.y = (int) (INIT_Y + (RAYON*Math.cos(angle_)) / (1 + (Math.sin(angle_)*Math.sin(angle_))));
		p.x = (int) (INIT_X + (RAYON*Math.cos(angle_)*Math.sin(angle_)) / (1 + (Math.sin(angle_)*Math.sin(angle_))));
		
		return p;
		
	}
	
	@Override
	public void onLoadComplete() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onPauseGame() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onResumeGame() {
		// TODO Auto-generated method stub
		
	}
	
}